package com.ssl.rms.exception;

import java.io.Serializable;

/**
 * 通用响应
 */
public class ApiResponse implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 是否超时 true 超时，false 正常
     */
    private Boolean overdue;
    /**
     * 结果码Integer/String
     */
    private Object flag;
    /**
     * 描述 //string
     */
    private Object message;
    /**
     * 返回数据
     */
    private Object result;

    /**
     * 构建成功的响应并设置响应数据
     *
     * @param data
     * @return
     */
    public static ApiResponse success(Object data) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setOverdue(false);
        apiResponse.setFlag(ResultFlagType.SUCCESS.getCode());
        apiResponse.setMessage("系统检测，本次执行成功!");
        apiResponse.setResult(data);
        return apiResponse;
    }

    /**
     * 构建超时的响应
     *
     * @param flag
     * @param message
     * @return
     */
    public static ApiResponse overdue(Object flag, String message) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setFlag(flag);
        apiResponse.setOverdue(true);
        apiResponse.setMessage(message);
        return apiResponse;
    }

    /**
     * 构建失败的响应
     *
     * @param flag
     * @param message
     * @return
     */
    public static ApiResponse fail(Object flag, String message) {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setFlag(flag);
        apiResponse.setOverdue(false);
        apiResponse.setMessage(message);
        return apiResponse;
    }

    /**
     * 构建失败的响应
     *
     * @param baseEnum
     * @return
     */
    public static ApiResponse fail(BaseEnum baseEnum) {
        ApiResponse apiResponse = new ApiResponse();
        if (null == baseEnum) {
            baseEnum = ResultFlagType.FAIL;
        }
        apiResponse.setOverdue(false);
        apiResponse.setFlag(baseEnum.getCode());
        apiResponse.setMessage(baseEnum.getValue());
        return apiResponse;
    }

    public Object getFlag() {
        return flag;
    }

    public void setFlag(Object flag) {
        this.flag = flag;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Boolean getOverdue() {
        return overdue;
    }

    public void setOverdue(Boolean overdue) {
        this.overdue = overdue;
    }
}
